<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'老师列表'),
	array('op'=>'edit','title'=>'添加老师'),	
);
$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');

$return['groups']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['area_id'])){
		$params['on_areas like']='%'.trim($_RQ['area_id']).'%';
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['lesson_groupid'])){
		$params['lesson_groupid']=intval($_RQ['lesson_groupid']);
		$return['params']['lesson_groupid']=$_RQ['lesson_groupid'];
	}
	if(intval($_RQ['page'])){
		$this->session('page',intval($_RQ['page']));
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','name','username','password','storeid','lesson_groupid','label','status','show','mid','headimg','on_areas','years','age','createtime','classid');
	$list=pdo_getslice('jy_kexie_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['store']=pdo_get('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']));
			$on_areas=str_array($item['on_areas']);
			$item['on_areas']='';
			$item['password']=base64_decode($item['password']);
			foreach($on_areas as $ov){
				$areatitle=pdo_getcolumn('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ov),'title');
				$item['on_areas'] .=$areatitle.',';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('total'=>$total,'data'=>'list','edit'=>true,'list'=>array(
			array('type'=>'img_string','title'=>'老师','img'=>'headimg',
			'data'=>array(
				array('title'=>'老师名称：','name'=>'name'),
				array('title'=>'用户名：','name'=>'username')
				)
			),
			array('name'=>'on_areas','title'=>'所属区域','type'=>'string'),
			array('name'=>'lesson_groupid','width'=>10,'title'=>'课程类型','type'=>'select','options'=>'groups'),
			array('name'=>'username','title'=>'账号','type'=>'string'),
			array('name'=>'password','title'=>'密码','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
		)),
		'search'=>array(
			array('name'=>'area_id','width'=>10,'title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'lesson_groupid','width'=>10,'title'=>'课程类型','type'=>'select','options'=>'groups'),
			array('name'=>'name','width'=>10,'title'=>'老师名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		unset($item['password']);
	}else{
		$item=array();		
	}
	$item['lesson']=pdo_get('jy_kexie_lesson',array('id'=>$item['lesson_id'],'uniacid'=>$_SESSION['uniacid']));
	//课程不存在了
	if(!$item['lesson']['id']){
		$item['lesson_id']=0;
	}
	$item['lesson']=$item['lesson']?:array('id'=>0);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回老师列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'headimg','title'=>'头像','type'=>'image'),
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'username','title'=>'用户名','type'=>'string'),
					array('name'=>'password','title'=>'密码','type'=>'password'),
					array('name'=>'mobile','title'=>'手机号','type'=>'string'),
					array('name'=>'tel','title'=>'固定电话','type'=>'string'),
					array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
					array('name'=>'years','title'=>'出生年月','type'=>'time','time_type'=>'day'),
					array('name'=>'code','title'=>'身份证号','type'=>'string'),
					array('name'=>'lesson_groupid','title'=>'课程类型','type'=>'select','options'=>'lessongroup'),
					array('name'=>'lesson.title','title'=>'课程名称','type'=>'string'),
					array('name'=>'lesson.on_num','title'=>'课时次数','type'=>'string'),
					array('type'=>'checkbox','title'=>'授课时间','name'=>'on_days','help'=>'','options'=>'on_days'),
					array('type'=>'checkbox','title'=>'授课区域','name'=>'on_areas','help'=>'','options'=>'areas'),
					//array('name'=>'on_num','title'=>'上课次数','type'=>'string'),
					array('name'=>'address','title'=>'现居住地','type'=>'string'),
					//array('name'=>'show','title'=>'讲课内容','type'=>'text'),
					array('name'=>'desc','title'=>'教师简介','type'=>'richtext'),
					array('name'=>'lessondesc','title'=>'课程简介','type'=>'richtext'),
					array('name'=>'remark','title'=>'备注','type'=>'text'),
					array('type'=>'choose','title'=>'绑定会员','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
		)
	);
	$return['on_days']=array('0'=>'周日','1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六');
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$areas=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']));
	foreach($areas as $av){
		$return['areas'][$av['id']]=$av['title'];
	}
	$return['lessongroup']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['lesson']=pdo_getall('jy_kexie_lesson',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['on_areas']){
		exi('请选择授课区域','error');
	}
	if(!$postdata['lesson_groupid']){
		exi('请选择课程类型','error');
	}
	if($postdata['lesson']['on_num'] && !$postdata['lesson']['title']){
		exi('请设置课程名称！','error');
	}
	//$postdata['time']=ijson_encode($postdata['time']);	
	$data=array_fields(array('address','code','sex','years','name','username','password','mobile','tel','lessondesc','lesson_id','desc','on_num','on_days','on_areas','headimg','show','remark','paixu','status','lesson_groupid','mid'),$postdata);
	$id=intval($postdata['id']);
	if($data['password']){
		$data['password']=base64_encode($data['password']);
	}
	if($data['years']){
		$data['years']=date('Y-m-d',strtotime($data['years']));
	}
	if($id){
		pdo_update('jy_kexie_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_kexie_worker',$data);
		$id=pdo_insertid();
	}
	if($postdata['lesson']['title']){
		$lesson=array(
			'title'=>$postdata['lesson']['title'],
			'on_num'=>$postdata['lesson']['on_num'],
			'groupid'=>$data['lesson_groupid'],
		);
		if($data['lesson_id']){
			pdo_update('jy_kexie_lesson',$lesson,array('id'=>$data['lesson_id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$lesson['uniacid']=$_SESSION['uniacid'];
			$lesson['workerid']=$id;
			pdo_insert('jy_kexie_lesson',$lesson);
			$lesson_id=pdo_insertid();
			pdo_update('jy_kexie_worker',array('lesson_id'=>$lesson_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！','','//list/page='.$this->session('page'));
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['on_areas like']='%'.trim($_RQ['area_id']).'%';
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['lesson_groupid'])){
		$params['lesson_groupid']=intval($_RQ['lesson_groupid']);
		$return['params']['lesson_groupid']=$_RQ['lesson_groupid'];
	}
	$lesson=pdo_getall('jy_kexie_worker',$params);
	$sex=array('1'=>'男','2'=>'女');
	foreach($lesson as &$item){
		$item['password']=base64_decode($item['password']);
		$item['grouptitle']=pdo_getcolumn('jy_kexie_lesson_group',array('id'=>$item['lesson_groupid']),'title');
		$on_areas=str_array($item['on_areas']);
		$item['on_areas']='';
		$item['createtime']=$item['createtime']?date('Y-m-d',$item['createtime']):'';
		foreach($on_areas as $ov){
				$areatitle=pdo_getcolumn('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ov),'title');
				$item['on_areas'] .=$areatitle.'，';
		}
		$item['week_daysnew']='';
		foreach($this->week_days as $k=>$d){
			if(strpos($item['on_days'],';'.$k.';') !== false){
				$item['week_daysnew'] .=$d.'，';
			}
		}
		$item['sex']=$sex[$item['sex']];
	}
	$header=array(
		'name'=>'姓名',
		'username'=>'账号',
		'password'=>'密码',
		'grouptitle'=>'课程类型',
		'sex'=>'性别',
		'years'=>'出生年月',
		'address'=>'居住地',
		'code'=>'身份证号',
		'mobile'=>'电话',
		'on_num'=>'上课次数',
		'show'=>'讲课内容',
		'week_daysnew'=>'上课时间',
		'on_areas'=>'上课区域',
		'createtime'=>'录入时间',
	);
	cfc('tools')->export2excel($header,$lesson,'教师表');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kexie_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的老师！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_kexie_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}